package com.translate.web.api.controller;

import com.translate.web.api.service.IMgOrderNodeRecordService;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestAttributes;

import com.response.vo.ResponseModel;
import com.returns.value.ICodeAndData;
import com.translate.web.api.logic.IPlanceOrderLogic;
import com.translate.web.constant.OrderErrorMsg;
import com.translate.web.param.ManuscriptAddParam;
import com.translate.web.param.PayManagerAddParam;
import com.translate.web.param.PayManagerServiceAddParam;

import javax.annotation.Resource;

/**
 * 下单
 */
@RestController
@RequestMapping("planceAnOrder")
public class PlanceAnOrderController {

	private IPlanceOrderLogic orderLogic;

	@Resource
	private IMgOrderNodeRecordService iMgOrderNodeRecordService;
	
	

	public PlanceAnOrderController(IPlanceOrderLogic orderLogic) {
		this.orderLogic = orderLogic;
	}

	/**
     * 下单
     * @param payManagerAddParam
     * @return
     */
	@PostMapping("placeAnOrder")
    public ResponseModel<Integer> placeAnOrder(
    		@RequestBody @Validated PayManagerAddParam<PayManagerServiceAddParam, ManuscriptAddParam>
    		payManagerAddParam, NativeWebRequest nativeWebRequest
    ) {
		long userId = (long) nativeWebRequest.getAttribute("userId", RequestAttributes.SCOPE_REQUEST);


		payManagerAddParam.setUserId((int) userId);
        ICodeAndData<Integer> codeAndData = orderLogic.placeAnOrder(payManagerAddParam);

        int placeAnOrder = codeAndData.getStatus();

        if (placeAnOrder == (OrderErrorMsg.ERROR_MSG.getCode())) {
        	return ResponseModel.errorMsg(placeAnOrder, codeAndData.getMsg());
        }

		return ResponseModel.success(codeAndData.getData());
    }
}
